﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VMC.Domain.Entity;
using VMC.Domain.Service;
using VMC.Domain.Service.Abstract;
using VMC.Domain.Model.Service.Abstract;
using VMC.Domain.Model.DataStructure;

namespace VMC.Domain.Model.Service
{
    public class ComplexDataTypeModelService : ModelService, IComplexDataTypeModelService
    {
        private IComplexDataTypeService iComplexDataType;
        private IComplexDataTypeColumnModelService iColumns;
        private ComplexDataTypeManagement mManagement;
        private string mUserName;

        public ComplexDataTypeModelService() : this("", new ComplexDataTypeService() , new ComplexDataTypeColumnModelService()) { }

        public ComplexDataTypeModelService(string User, IComplexDataTypeService Service, IComplexDataTypeColumnModelService ColumnService)
        {
            iComplexDataType = Service;
            iColumns = ColumnService;
            mManagement = new ComplexDataTypeManagement();
            UserName = User;
            //GetList(UserName);
        }
        

        #region "Functions related to Complex DataType it self"

     
        public string UserName { get { return mUserName; } set { mUserName = value; GetList(mUserName); } }
        
        public ComplexDataTypeManagement GetList(string UserName)
        {
            mManagement.Reset();
            mManagement.Items = new ComplexDataTypeList();
            List<AddEditComplexDataType> mComplexDataTypeList = new List<AddEditComplexDataType>();
            foreach (ComplexDataType mComplex in iComplexDataType.All(UserName))
            {

                AddEditComplexDataType mTemp = new AddEditComplexDataType();

                mTemp.Comment = mComplex.Comment;
                mTemp.Description = mComplex.Description;
                mTemp.ID = mComplex.ID;
                mTemp.IsPublic = mComplex.IsPublic;
                mTemp.Name = mComplex.Name;
                mTemp.UserName = mComplex.User.UserName;
                mTemp.VariableName = mComplex.VariableName;
                //Code for mapping
                mComplexDataTypeList.Add(mTemp);
            }
            mManagement.Items.List = mComplexDataTypeList.ToList();
            return mManagement;
        }
        
        public ComplexDataTypeManagement GetCreateModel()
        {
            mManagement.AddEditModel = new AddEditComplexDataType();
            mManagement.AddEditModel.AutoGenerateVaribleName = true;
            return mManagement;
        }
        
        public ComplexDataTypeManagement GetCurrentModel(AddEditComplexDataType Model)
        {
            mManagement.AddEditModel = Model;
            return mManagement;
        }
        
        public bool Create(AddEditComplexDataType Model)
        {
            mManagement.AddEditModel = Model;
            //Imlement this
            return iComplexDataType.Create(Model.UserName, Model.Name, Model.VariableName, Model.Description, Model.Comment, Model.IsPublic);
            //return false;
        }
        
        public ComplexDataTypeManagement GetUpdateModel(int ID)
        {
            mManagement.Items.SelectedIndex = ID;
            ComplexDataType mComplex = iComplexDataType.Find(ID);
            if (mComplex != null && !mComplex.IsPublic)
            {
                mManagement.AddEditModel = new AddEditComplexDataType();
                mManagement.AddEditModel.Comment = mComplex.Comment;
                mManagement.AddEditModel.Description = mComplex.Description;
                mManagement.AddEditModel.ID = mComplex.ID;
                mManagement.AddEditModel.IsPublic = mComplex.IsPublic;
                mManagement.AddEditModel.Name = mComplex.Name;
                mManagement.AddEditModel.UserName = mComplex.User.UserName;
                mManagement.AddEditModel.VariableName = mComplex.VariableName;
            }
            return mManagement;
        }
        
        public bool Update(AddEditComplexDataType Model)
        {
            mManagement.Items.SelectedIndex = Model.ID;
            return iComplexDataType.Update(Model.ID, Model.Name, Model.VariableName, Model.Description, Model.Comment, Model.IsPublic);
        }
        
        public bool Delete(int ID)
        {
            //Implement this
            iColumns.DeleteColumns(ID);
            return iComplexDataType.Delete(ID);
        }
        
        public override Dictionary<string, string> GetErrors()
        {
            return iComplexDataType.GetValidationErrors();
        }

        #endregion

        #region "Function related to Complex Datatype Columns"
        //These set of functions will be just an abstract level implementation of the actual model service
        //It will still return ComplexDataTypeManagement as result in most cases.
        public ComplexDataTypeManagement GetColumnsForEditing(int ID, string UserName)
        {
            //Before loading the list check for the ownership for the item
            if (iComplexDataType.Find(ID) != null && iComplexDataType.Find(ID).User.UserName == UserName)
            {
                mManagement.Items.SelectedIndex = ID;
                mManagement.ColumnManagement = new ComplexDataTypeColumnManagement();
                mManagement.ColumnManagement = iColumns.GetList(ID);
                mManagement.ColumnManagement.AddEditModel = new AddEditComplexDataTypeColumn();
                mManagement.ColumnManagement.AddEditModel.AutoGenerateVaribleName = true;
                mManagement.ColumnManagement.AddEditModel.ComplexDataTypeID = ID;
            }

            return mManagement;
        }


        public bool AddBasicDataTypeColumn(AddEditComplexDataTypeColumn Model)
        {
            //bool mStatus = false;
            return iColumns.Create(Model);
            //return mStatus;
        }

        public bool UpdateBasicDataTypeColumn(AddEditComplexDataTypeColumn Model)
        {
            return iColumns.Update(Model);
        }
        public bool UpdateColumnOrder(int ComplexDataTypeID, int ColumnID, int UpdatedOrder)
        {
            return iColumns.UpdateColumnOrder(ComplexDataTypeID, ColumnID, UpdatedOrder);
        }

        public ComplexDataTypeManagement GetUpdateColumnModel(int id, string UserName)
        {
            mManagement.ColumnManagement = iColumns.GetUpdateModel(id);
            return mManagement;
        }

        public bool DeleteBasicDataTypeColumn(int ID)
        {
            bool mStatus = false;
            mStatus = iColumns.Delete(ID);
            return mStatus;
        }

        public void RefreshColumnOrder(int ID)
        {
            int i = 1;
            foreach (AddEditComplexDataTypeColumn mColumn in iColumns.GetList(ID).Items.List)
            {
                if (mColumn.ColumnOrder != i)
                {
                    iColumns.UpdateColumnOrder(ID, mColumn.ID, i);
                }
                i++;
            }
        }
        #endregion
    }
}
